/**
 *
 */
package com.googlecode.pkchat.pkserver;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;

/**
 * @author Simon Pratt
 *
 */
public class ServerThread extends Thread {

	Server server;
	UserSocket socket;

	/**
	 *
	 */
	public ServerThread(Server server, UserSocket socket) {
		this.server = server;
		this.socket = socket;
		start();
	}

	public void run(){
		try{
			DataInputStream din = new DataInputStream(socket.getInputStream());
			while(true){
				String message = din.readUTF();
				if(message.startsWith("/")){
					if(command(message).startsWith("n")){
						String[] messages = parseCommand(message);
						server.sendToAll(socket.getName() + " changed name to "+messages[1]);
						socket.setName(messages[1]);
						server.broadcastUsers();
					}
					else if(command(message).startsWith("w")){
						String[] messages = parseCommand(message);
						if(messages.length<2) server.serverMessage(socket.getName(),"Please enter a whisper recipient.");
						else if(messages.length<3) server.serverMessage(socket.getName(),"Please enter a message to whisper.");
						else{
							message = getMessage(message);
							server.whisper(messages[1], socket.getName(), message);
						}
					}
					else if(command(message).startsWith("em") || command(message).startsWith("me")){
						server.sendToAll(socket.getName() + " " + message.substring(message.indexOf(' ')+1));
					}
					else if(command(message).startsWith("t")){
						server.sendToAll(socket.getName() + ".oO("+message.substring(message.indexOf(' ')+1)+")");
					}
					else{
						server.serverMessage(socket.getName(), "Unrecognized command.");
					}
				}
				else
					server.sendToAll(socket.getName() + " says: " + message);
			}
		}catch (EOFException eof){

		}
		catch (IOException e){
			e.printStackTrace();
		}
		finally{
			server.removeConnection(socket);
		}
	}

	/**
	 * @param target
	 */
	public ServerThread(Runnable target) {
		super(target);
	}

	/**
	 * @param name
	 */
	public ServerThread(String name) {
		super(name);
	}

	/**
	 * @param group
	 * @param target
	 */
	public ServerThread(ThreadGroup group, Runnable target) {
		super(group, target);
	}

	/**
	 * @param group
	 * @param name
	 */
	public ServerThread(ThreadGroup group, String name) {
		super(group, name);
	}

	/**
	 * @param target
	 * @param name
	 */
	public ServerThread(Runnable target, String name) {
		super(target, name);
	}

	/**
	 * @param group
	 * @param target
	 * @param name
	 */
	public ServerThread(ThreadGroup group, Runnable target, String name) {
		super(group, target, name);
	}

	/**
	 * @param group
	 * @param target
	 * @param name
	 * @param stackSize
	 */
	public ServerThread(ThreadGroup group, Runnable target, String name,
			long stackSize) {
		super(group, target, name, stackSize);
	}

	private String command(String message){
		return message.substring(1).toLowerCase();
	}

	private String[] parseCommand(String message){
		return message.split(" ");
	}
	private String getMessage(String message){
		message = message.substring(message.indexOf(' ')+1);
		message = message.substring(message.indexOf(' ')+1);
		return message;
	}
}
